<?php

namespace app\api\model;

use think\Model;

use app\api\model\Token;
use think\facade\Db;
use think\model\concern\SoftDelete;

/**
 * 用户模型
 */
class User extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';


    // protected $name='servers_user';

    // 开启自动写入时间戳字段
    protected $auto_timestamp = 'false';

     //用户未点击完成服务，72小时自动完成
     public function complete_theservice(){
        Db::name('order')
        ->where('status',3)
        ->where('is_complete_service',0)
        ->field("id,status,is_complete_service,paidin_money,uid")
        ->chunk(100, function($users) {
            foreach ($users as $user) {
                //处理逻辑
                $order_scanning_code = Db::name("order_scanning_code")->where("oid",$user["id"])->where("update_time",">",0)->find();
                if(!empty($order_scanning_code)){
                    $houretime = strtotime("72 hour",$order_scanning_code["update_time"]);
                    //$houretime = date("Y-m-d H:i:s",strtotime("72 hour",$order_scanning_code["update_time"]));
                    //判断当前时间是否大于72小时
                    if(time() > $houretime){
                        Db::name("order")->where("id",$user["id"])->update(["status"=>4,"is_complete_service"=>1,"completion_time"=>time()]);
                        //状态修改成功，给上级返佣

                        //查询订单技师，更新技师佣金
                        $order_scanning_code = Db::name("order_scanning_code")->where("oid",$user["id"])->select()->toArray();
                        if(!empty($order_scanning_code)){
                            foreach($order_scanning_code as $k=>$v){
                                //查询技师提成比例
                                $service_technician = Db::name("service_technician")->where("id",$v["service_user_id"])->find();
                                $royalty_ratio = bcdiv($service_technician["royalty_ratio"],100,2);
                
                                //更新技师佣金
                                $techn_money = bcmul($user["paidin_money"],$royalty_ratio,2);
                                Db::name("service_technician")->where("id",$v["service_user_id"])->inc("commission",$techn_money)->update();
                
                                //技师佣金记录
                                $ji_data = [
                                    "sid"=>$v["service_user_id"],
                                    "royalty_ratio"=>$royalty_ratio,
                                    "money"=>$techn_money,
                                    "uid"=>$user["uid"],
                                    "oid"=>$user["id"],
                                    "type"=>1,
                                    "desc"=>"订单完成给技师返回佣金",
                                    "create_time"=>time()
                                ];
                                Db::name("commission_record")->insert($ji_data);
                            }
                        }
            
                        //查询分销比例
                        $distribution_commission_ratio = sysconfig('contact', 'distribution_commission_ratio');
                        $distribution_commission_ratio = bcdiv($distribution_commission_ratio,100,2);
            
                        //更新分销商佣金
                        $userarr = Db::name("user")->where("id",$user["uid"])->find();
                        if($userarr["superior_fid"] > 0){
                            $fxs_money = bcmul($user["paidin_money"],$distribution_commission_ratio,2);
                            Db::name("distributor")->where("id",$userarr["superior_fid"])->inc("commission",$fxs_money)->update();
                            //分销商佣金记录
                            $fxs_data = [
                                "superior_fid"=>$userarr["superior_fid"],
                                "distribution_commission_ratio"=>$distribution_commission_ratio,
                                "money"=>$fxs_money,
                                "uid"=>$user["uid"],
                                "oid"=>$user["id"],
                                "type"=>2,
                                "desc"=>"订单完成给上级返回佣金",
                                "create_time"=>time()
                            ];
                            Db::name("commission_record")->insert($fxs_data);
                        }

                    }
                }
            }
        });
        return ["code"=>1];
     }
     
    //设备方向控制---后台调用
    public function admin_direction_control($Command,$equi_id){
        if(empty($Command)){
            return false;
        }
        $serial_number = Db::name("equipment")->where("id",$equi_id)->find();
        //获取设备token
        $token = $this->get_device_token($serial_number["equi_name"]);
        if($token["code"] != 1){
            return false;
        }
        $sbeitoken = $token["data"][0]["token"];
        //重新登录设备
        $device_login = $this->device_login($sbeitoken,$serial_number);
        if($device_login["code"] != 1){
            return false;
        }
      

        $method = 'POST';
        $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
        $header = [
                    'Content-Type:application/json;charset=UTF-8'
                  ];
                  
        $qwe = [
            "Name"=>"OPPTZControl",
            "OPPTZControl"=>[
                "Command"=>$Command,
                "Parameter"=>[
                    "Preset"=>65535,//65535
                    "Channel"=>0,
                    "Step"=>3
                ]
            ]
        ];
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['code'] == 2000)
       {
            return true;
       }else{
            return false;
        }
    }

     //设备方向控制运动停止
     public function admin_stop_equipment_control($Command,$equi_id){
        if(empty($Command)){
            return false;
        }
        $serial_number = Db::name("equipment")->where("id",$equi_id)->find();
        //获取设备token
        $token = $this->get_device_token($serial_number["equi_name"]);
        if($token["code"] != 1){
            return false;
        }
        $sbeitoken = $token["data"][0]["token"];
        //重新登录设备
        $device_login = $this->device_login($sbeitoken,$serial_number);
        if($device_login["code"] != 1){
            return false;
        }

        $method = 'POST';
        $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
        $header = [
                    'Content-Type:application/json;charset=UTF-8'
                  ];
                  
        $qwe = [
            "Name"=>"OPPTZControl",
            "OPPTZControl"=>[
                "Command"=>$Command,
                "Parameter"=>[
                    "Preset"=>-1,
                    "Channel"=>0,
                    "Step"=>1
                ]
            ]
        ];
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['code'] ==2000)
       {
            return true;
        }else{
            return false;
        }
    }

    //预置点转到左
    public function cruise_left(){
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",1)->field("id,equi_id,patfx_type_id")->find();
          
            if(!empty($patrol_direction)){
                 //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
           
          
        }
        return ["code"=>1];
       // dump(123);die;
    }

     //预置点转到右
     public function cruise_right(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",2)->field("id,equi_id,patfx_type_id")->find();
            //$v["id"]
            if(!empty($patrol_direction)){
                //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
              
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
         
          
        }
        return ["code"=>1];
        //dump(123);die;
    }

     //预置点转到上
     public function cruise_upper(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",3)->field("id,equi_id,patfx_type_id")->find();
            if(!empty($patrol_direction)){
                 //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
           
          
        }
        return ["code"=>1];
       // dump(123);die;
    }

     //预置点转到下
     public function cruise_lower(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",4)->field("id,equi_id,patfx_type_id")->find();
            if(!empty($patrol_direction)){
                  //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
          
          
        }
        return ["code"=>1];
       // dump(123);die;
    }

     //预置点转到左上
     public function cruise_topleft(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",5)->field("id,equi_id,patfx_type_id")->find();
            if(!empty($patrol_direction)){
                 //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
           
          
        }
        return ["code"=>1];
       // dump(123);die;
    }

    //预置点转到左下
    public function cruise_bottomleft(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",6)->field("id,equi_id,patfx_type_id")->find();
            if(!empty($patrol_direction)){
                 //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
           
          
        }
        return ["code"=>1];
       // dump(123);die;
    }

     //预置点转到右上
     public function cruise_topright(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",7)->field("id,equi_id,patfx_type_id")->find();
            if(!empty($patrol_direction)){
                 //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
           
          
        }
        return ["code"=>1];
       // dump(123);die;
    }

     //预置点转到右下
     public function cruise_lowerright(){
        // 1左   2右  3上  4下  5左上  6左下 7右上  8右下
        $equipment = Db::name("equipment")->order("id desc")->select()->toArray();
        foreach($equipment as $k=>$v){
            //巡航方向
            $patrol_direction = Db::name("patrol_direction")->where("equi_id",$v["id"])->where("patfx_type_id",8)->field("id,equi_id,patfx_type_id")->find();
            if(!empty($patrol_direction)){
                //设备信息
                $serial_number = Db::name("equipment")->where("id",$patrol_direction["equi_id"])->find();
                //预置点
                $set_preset_point = Db::name("set_preset_point")->where("patrol_direction_id",$patrol_direction["id"])->where("equi_id",$patrol_direction["equi_id"])->find();
                if(!empty($set_preset_point)){
                    $Preset = $set_preset_point["flight_number"];
                    //获取设备token
                    $token = $this->get_device_token($serial_number["equi_name"]);
                    // if($token["code"] != 1){
                    //     return "参数错误1";
                    // }
                    $sbeitoken = $token["data"][0]["token"];
                    //重新登录设备
                    $device_login = $this->device_login($sbeitoken,$serial_number);
                    // if($device_login["code"] != 1){
                    //     return "参数错误2";
                    // }
                    $method = 'POST';
                    $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
                    $header = [
                                'Content-Type:application/json;charset=UTF-8'
                            ];
                    $qwe = [
                        "Name"=>"OPPTZControl",
                        "OPPTZControl"=>[
                            "Command"=>"GotoPreset",
                            "Parameter"=>[
                                "Preset"=>$Preset,
                                "Channel"=>0,
                                "PresetName"=>1
                            ]
                        ]
                    ];
                $res =  json_encode($qwe);
                $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
                }
            }
           
          
        }
        return ["code"=>1];
       // dump(123);die;
    }







     //设置预置点
     public function set_preset_point($Preset,$equi_id){
        if(empty($Preset)){
            return false;
        }
        $Preset = intval($Preset);
        $serial_number = Db::name("equipment")->where("id",$equi_id)->find();
        //获取设备token
        $token = $this->get_device_token($serial_number["equi_name"]);
        if($token["code"] != 1){
            $this->error('参数错误1');
        }
        $sbeitoken = $token["data"][0]["token"];
        //重新登录设备
        $device_login = $this->device_login($sbeitoken,$serial_number);
        if($device_login["code"] != 1){
            $this->error('参数错误2');
        }

        $method = 'POST';
        $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
        $header = [
                    'Content-Type:application/json;charset=UTF-8'
                  ];
                  
        $qwe = [
            "Name"=>"OPPTZControl",
            "OPPTZControl"=>[
                "Command"=>"SetPreset",
                "Parameter"=>[
                    "Preset"=>$Preset,
                    "Channel"=>0,
                    "PresetName"=>1
                ]
            ]
        ];
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['code'] == 2000)
       {
            return true;
       }else{
            return false;
       }
    }

    //删除预置点
    public function del_preset_point($Preset,$equi_id){
        if(empty($Preset)){
            return false;
        }
        $Preset = intval($Preset);
        $serial_number = Db::name("equipment")->where("id",$equi_id)->find();
        //获取设备token
        $token = $this->get_device_token($serial_number["equi_name"]);
        if($token["code"] != 1){
            $this->error('参数错误1');
        }
        $sbeitoken = $token["data"][0]["token"];
        //重新登录设备
        $device_login = $this->device_login($sbeitoken,$serial_number);
        if($device_login["code"] != 1){
            $this->error('参数错误2');
        }

        $method = 'POST';
        $url = 'https://rds.bcloud365.net/v2/rtc/device/opdev/'.$sbeitoken;
        $header = [
                    'Content-Type:application/json;charset=UTF-8'
                  ];
                  
        $qwe = [
            "Name"=>"OPPTZControl",
            "OPPTZControl"=>[
                "Command"=>"ClearPreset",
                "Parameter"=>[
                    "Preset"=>$Preset,
                    "Channel"=>0,
                    "PresetName"=>1
                ]
            ]
        ];
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['code'] == 2000)
       {
            return true;
       }else{
            return false;
       }
    }

     //获取设备token
     public function get_device_token($newsns){
        $sns = explode(",",$newsns);
      
        $method = 'POST';
        $url = 'https://tks.xmeye.net/v2/device/token/00000011645153792342/25e582d562ca7a5c84eb115ed1b3554a.rs';
        $header = [
                    'Content-Type:application/json;charset=UTF-8',
                    'uuid:630ecdf2127490150da0df4e',
                    'appKey:319054034924c5a91562d2ed6eb85a2f'
                  ];
  
       $qwe = [
           "sns"=>$sns,
           "userId"=>""
       ];
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['code'] ==2000 && $data['msg'] == 'SUCCESS' )
       {    
           return [
               "code"=>1,
               "data"=>$data["data"]
           ];
           // $this->success('获取成功',$data["data"]);
       }else{
            return [
                "code"=>-1,
                "data"=>""
            ];
           // $this->error('获取失败');
        }
    }

      //设备登录
      public function device_login($token,$equi){
       
        $method = 'POST';
        $url = 'https://rds.bcloud365.net/v2/rtc/device/login/'.$token;
        $header = [
                    'Content-Type:application/json;charset=UTF-8'
                  ];
                  
        $qwe = [
           "EncryptType"=>"MD5",
           "LoginType"=>"DVRIP-Web",
           "PassWord"=>$equi["equi_pass"],
           "UserName"=>$equi["equi_contne"],
           "Name"=>"generalinfo"
        ];
       $res =  json_encode($qwe);
       $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['code'] ==2000)
       {
            return [
                "code"=>1,
                "data"=>$data["data"]
            ];
           // $this->success('获取成功',$data["data"]);
       }else{
            return [
                "code"=>-1,
                "data"=>$data
            ];
           // $this->error('获取失败');
        }
    }

      /**
    * 发送 post 请求 
    * @access protected
    * @return string
    */
    protected function http_curl($url, $method = 'POST', $data = array(), $header, $timeout = 10)
   {
       $curl = curl_init($url);
       $method = strtoupper($method);
       //请求方式啥
       curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
       //post请求
       if ($method == 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
       //超时时间
       curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
       //设置header头
       // if ($header !== false) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
       curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
       
       curl_setopt($curl, CURLOPT_FAILONERROR, false);
       //返回抓取数据
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       //输出header头信息
       curl_setopt($curl, CURLOPT_HEADER, true);
       //TRUE 时追踪句柄的请求字符串，从 PHP 5.1.3 开始可用。这个很关键，就是允许你查看请求header
       curl_setopt($curl, CURLINFO_HEADER_OUT, true);

       //https请求
       if (1 == strpos("$" . $url, "https://")) {
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
           curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
       }
       $curlError = curl_error($curl);
     
       list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
       $status = $status;
       
       $headerStr = trim(substr($content, 0, $status['header_size']));
       $content = trim(substr($content, $status['header_size']));
       
        $cont = json_decode($content,true);
        
       //  $cont['headers'] =$headerStr;   //返回的头部信息去掉
        return $cont;

   }



}
